public class DocumentManager : Gtk.Notebook 
{
	private static DocumentManager instance = null; 	
	
	public weak Document current_doc 
	{
		get { return (Document) this.get_nth_page (this.get_current_page ()); }
	}

	construct
	{
		this.page_added += this.on_page_added;
		this.page_removed += this.on_page_removed;
	}
	
	private DocumentManager ()
	{
	} 
	
	//Tworzy dokument 
	public bool create (string? filename = null)
	{
		if (
			filename != null &&
			this.current_doc != null &&
			this.current_doc.state == Document.DocumentState.NO_MODIFIED && 
			this.current_doc.get_content ().length == 0
		)
		{
			this.current_doc.set_contents_from_file (filename);			
			((DocumentHeader) this.get_tab_label (this.current_doc)).text = GLib.Path.get_basename (filename);
		}
		else
		{
			var doc = new Document (filename);
			this.set_current_page (
				this.append_page (doc, new DocumentHeader (filename, ref doc))
			);				
			this.set_tab_reorderable (doc, true);		
			doc.grab_focus ();
		}		
		return true;		
	}
	
	//Otwiera plik za pomocą okna...
	public void open ()
	{
		Gtk.FileChooserDialog dialog = new Gtk.FileChooserDialog (
			"Otwórz...", null,
			Gtk.FileChooserAction.OPEN,
			Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
			Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT
		);
		dialog.set_select_multiple (true);
		
		Gtk.FileFilter filter = new Gtk.FileFilter ();
		filter.add_mime_type ("text/*");
		
		dialog.set_filter (filter);
		
		if (dialog.run () == Gtk.ResponseType.ACCEPT)
		{
			weak GLib.SList<string> list = dialog.get_filenames (); 
			foreach (string file in list)
			{
				this.create (file);
			}
		}
	
		dialog.destroy ();
	}

	public void save_current () 
	{
		current_doc.save ();	
	}
	
	public void save_as_current ()
	{
		current_doc.save_as ();			
	}
	
	public void save_all ()
	{
		for (int i = this.get_n_pages () - 1; i >= 0; --i)
		{
			var doc = (Document) this.get_nth_page (i);
			doc.save ();
		}
	}
	
	//Zamyka aktualny dokumnt
	public void close_current ()
	{
		if (current_doc.close ())
		{
			this.remove_page (this.get_current_page ());
		}		
	}
	
	//Zamyka wszytskie otwarte dokumenty
	//Zwraca false gdy anulowano zamykanie, true gdy wszytsko sie powiodło
	public bool close_all ()
	{
		for (int i = this.get_n_pages () - 1; i >= 0; --i)
		{
			this.set_current_page (i);		
	
			var doc = (Document) this.get_nth_page (i);
			if (doc.close ())
			{
				this.remove_page (i);
				doc.destroy ();
			}
			else return false;
		}
		return true;
	}
	
	public void undo_current ()
	{
		current_doc.undo ();
	}

	public void redo_current ()
	{
		current_doc.redo ();
	}
	
	public void cut_current ()
	{
		current_doc.cut ();
	}
	
	public void copy_current ()
	{
		current_doc.copy ();
	}
	
	public void paste_current ()
	{
		current_doc.paste ();
	}	
	
	public void delete_current ()
	{
		current_doc.delete_selection ();
	}
	
	public void select_all_current ()
	{
		current_doc.select_all ();
	}
	
	public void search_in_current ()
	{
		current_doc.show_search_panel ();
	}
	
	public void replace_in_current ()
	{
		current_doc.show_replace_panel ();
	}
	
	public void goto_line_current ()
	{
		current_doc.show_goto_line_panel ();
	}
	
	public void goto_next_found_match_current ()
	{
		current_doc.goto_next_found_match ();
	}

	public void goto_prev_found_match_current ()
	{
		current_doc.goto_prev_found_match ();
	}
	
	public static weak DocumentManager get_instance ()
	{
		if (instance == null)
		{
			instance = new DocumentManager ();
		}
		return instance;
	}
	
	protected void on_page_added (DocumentManager document_manager, Gtk.Widget p0, uint p1)
	{
		if (this.get_n_pages() == 1)
		{
			UIManager.get_instance().set_sensitive_doc_actions (true);
		}
	}
	
	protected void on_page_removed (DocumentManager document_manager, Gtk.Widget p0, uint p1)
	{
		if (this.get_n_pages() == 0)
		{
			UIManager.get_instance().set_sensitive_doc_actions (false);
		}
	}
}
